package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.ruicingmarket.resource.ApiGroup;
import org.rcy.ruicingmarket.role.RoleQueryCondition;
import org.rcy.ruicingmarket.role.RoleVo;
import org.rcy.ruicingmarket.role.BindViewsVo;
import org.rcy.ruicingmarket.role.BindUsersVo;
import org.rcy.ruicingmarket.security.PrivilegeCheck;
import org.rcy.ruicingmarket.exception.OssException;
import org.rcy.ruicingmarket.oss.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/10/12 11:00
 */

@RestController
@RequestMapping("/role")
@Tag(name = "ossrole", description = "后台角色控制器")
public class OssRoleController {
	@Autowired
	private RoleService roleService;

	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "添加角色",auth = true)
	@PostMapping("/addrole")
	@Operation(summary = "添加角色", description = "添加角色", tags = {"ossrole"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	public ResultMessage addRole(@RequestBody RoleVo vo) {
		try {
			roleService.addRole(vo);
			return ResultMessage.success();
		}catch (OssException e){
			return ResultMessage.error();
		}
	}

	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "为用户绑定角色",auth = true)
	@PostMapping("/bindusers")
	@Operation(summary = "为用户绑定角色", description = "为用户绑定角色", tags = {"ossrole"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	public ResultMessage bindUsers(@RequestBody BindUsersVo vo) {
		try {
			roleService.bindUsers(vo);
			return ResultMessage.success();
		}catch (OssException e){
			return ResultMessage.error();
		}
	}

	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "为角色绑定视图资源",auth = true)
	@Operation(summary = "为角色绑定视图资源", description = "为角色绑定视图资源", tags = {"ossrole"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	@PostMapping("/bindviews")
	public ResultMessage bindMenus(@RequestBody BindViewsVo vo){
		try {
			roleService.bindViews(vo);
			return ResultMessage.success();
		}catch (OssException e){
			return ResultMessage.error();
		}
	}

	@PrivilegeCheck(group = ApiGroup.SYSTEM,name = "分页查询角色",auth = true)
	@Operation(summary = "分页查询角色", description = "分页查询角色", tags = {"ossrole"})
	@Parameter(name = "ossInnerToken", required = true , in = ParameterIn.HEADER)
	@PostMapping("/query")
	public ResultMessage<PageResult<RoleVo>> pageQuery(@RequestBody RoleQueryCondition condition){
		try {
			return ResultMessage.success(roleService.pageQuery(condition));
		}catch (OssException e){
			return ResultMessage.error();
		}
	}
}
